package com.htdata.service;

import com.casic.iot.client.DeviceStateClient;
import com.casic.iot.model.request.DeviceStateRequest;
import com.casic.iot.model.response.DeviceStateResponse;
import com.htdata.util.ExcelUtil;
import com.htdata.util.PropUtil;

import jxl.read.biff.BiffException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.Thread;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by T数据部 on 2017/5/26.
 */
public class DevStatus2Cloud extends Thread {
    private Map config =new HashMap();
    public DevStatus2Cloud(Map config){
        this.config=config;
    }
    public DevStatus2Cloud(){
    }
    String url="http://iotapi.casicloud.com/api/1.1/iot/device_state_acquire ";
    String[] stas = {"1000","1000","2000","3000","1000","1000"};

    public void run() {
        //DeviceStateClient client = new DeviceStateClient(url, (String) config.get("accesskey"));
       // DeviceStateRequest req = new DeviceStateRequest();
       // req.setIot((String) config.get("iot"));
        List list = null;
        try {
			list = ExcelUtil.createPropertyByExcel();
		} catch (BiffException | IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        //是否已经清除nohup.out里的内容
        boolean flag = false;
		while (true) {
			Date day=new Date();    
			SimpleDateFormat df1 = new SimpleDateFormat("HH"); 
			int hour = Integer.parseInt(df1.format(day));
			//8点-18点发送
			if(hour >= 8 && hour < 23){
				flag = false;
				if (list != null && list.size() > 0) {
					//System.out.println("list size =="+list.size());
					for (int i = 0; i < list.size(); i++) {
						Map map = (Map) list.get(i);
						String accesskey = ((String) map.get("apikey")).trim();
						String iot = ((String) map.get("iot")).trim();

						DeviceStateClient client = new DeviceStateClient(url, accesskey);
						DeviceStateRequest req = new DeviceStateRequest();
						req.setIot(iot);

						String equipmentStr = map.get("equipment").toString();
						long ts = System.currentTimeMillis();
						// String[] eqs = PropUtil.getRunValue((String)
						// config.get("iot")).split(",");
						String[] eqs = equipmentStr.split(",");
						for (String str : eqs) {
							int t = (int) Math.random() * 6;
							req.addData(str.trim(), stas[t], ts);
						}

						DeviceStateResponse result = client.execute(req);

						System.out.println("putin status");
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						System.out.println(df.format(new Date()));// new
					}
					//全部发送完成后休眠3分钟
					try {
						Thread.sleep(180 * 1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					//循环完清空
					String path = DevStatus2Cloud.class.getClassLoader().getResource("").getPath();
					File f = new File(path+"/nohup.out");
					FileWriter fw;
					try {
						fw = new FileWriter(f);
						fw.write("");
						flag = true;
						fw.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			} /*else if (hour == 1){
				if(!flag){
					String path = DevStatus2Cloud.class.getClassLoader().getResource("").getPath();
					File f = new File(path+"/nohup.out");
					FileWriter fw;
					try {
						fw = new FileWriter(f);
						fw.write("");
						flag = true;
						fw.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					System.out.println("清空内容");
				}
			}*/
		}
    }
}
